﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Comm;
using VehicleMaintenance.Models;
using VehicleMaintenance.Events;

namespace VehicleMaintenance.BaseInfo
{
    public partial class OtherExpenditureForm : BaseForm
    {

        #region 私有变量及属性

        /// <summary>
        /// 当前窗口状态
        /// </summary>
        public State state = State.Add;

        /// <summary>
        /// 记录当前支出日期
        /// </summary>
        private string date = DateTime.Now.ToString("yyyy-MM");


        #endregion 私有变量及属性

        public OtherExpenditureForm()
        {
            InitializeComponent();

            InitializeElements();
        }

        public OtherExpenditureForm(string dt)
        {
            InitializeComponent();

            InitializeElements();

            date = dt;
            state = State.Edit;
        }

        private void OtherExpenditureForm_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.state == State.Edit)
                {
                    CarDBContext db = new CarDBContext();

                    var expenditure = db.OtherExpenditure.Where(p => p.Date == this.date);
                    if (expenditure != null)
                    {
                        dtPicker.Value = DateTime.Parse(date);
                        dtPicker.Enabled = false;
                        for (int i = 0; i < tabPanel.Controls.Count; i += 3)
                        {
                            OtherExpenditure dto = expenditure.First(p => p.Type == tabPanel.Controls[i].Text);
                            if (dto != null)
                            {
                                tabPanel.Controls[i + 1].Text = dto.Amount.ToString();
                                tabPanel.Controls[i + 2].Text = dto.Units;
                            }
                            else
                            {
                                tabPanel.Controls[i + 1].Text = "0";
                                tabPanel.Controls[i + 2].Text = "元";
                            }
                        }

                    }
                }
                else
                {
                    InitializeDefault();
                }
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                CarDBContext db = new CarDBContext();

                if (this.state == State.Edit)
                {
                    //首先删除数据库中存的当日的支出数据
                    string sql = "delete from OtherExpenditure where date=@date";
                    MySql.Data.MySqlClient.MySqlParameter parameter = new MySql.Data.MySqlClient.MySqlParameter();
                    parameter.DbType = DbType.String;
                    parameter.Direction = ParameterDirection.Input;
                    parameter.ParameterName = "@date";
                    parameter.Size = 10;
                    parameter.Value = dtPicker.Value.ToString("yyyy-MM");
                    db.Database.ExecuteSqlCommand(sql, parameter);

                    db.SaveChanges();
                }
                else
                {
                    //查询日期是否重复
                    string date = dtPicker.Value.ToString("yyyy-MM");
                    if (db.OtherExpenditure.FirstOrDefault(p => p.Date == date) != null)
                    {
                        this.ShowInfo("不能添加本月支出\r\n因为本月支出已经存在\r\n请找到本月支出编辑");
                        //tabPanel.Controls["Code"]();
                        return;
                    }

                }
                for (int i = 0; i < tabPanel.Controls.Count; i += 3)
                {
                    OtherExpenditure dto = new OtherExpenditure();
                    dto.Date = dtPicker.Value.ToString("yyyy-MM");
                    decimal amount = 0;
                    decimal.TryParse(tabPanel.Controls[i + 1].Text, out amount);
                    dto.Amount = amount;
                    dto.Type = tabPanel.Controls[i].Text;
                    dto.Units = tabPanel.Controls[i + 2].Text;
                    db.OtherExpenditure.Add(dto);
                }
                db.SaveChanges();

                this.ShowInfo("保存成功！");
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void InitializeElements()
        {
            string[] strType = System.Configuration.ConfigurationManager.AppSettings["OtherExpenditureType"].Split(',');

            for (int i = 0; i < strType.Length; i++)
            {
                Label lb = new Label();
                lb.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                lb.TextAlign = ContentAlignment.MiddleRight;
                lb.Text = strType[i];
                lb.Width = 20;
                tabPanel.Controls.Add(lb);

                TextBox txt = new TextBox();
                txt.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                txt.Name = strType[i].Trim();
                txt.Margin = new Padding(0, 0, 20, 0);
                txt.KeyPress += new KeyPressEventHandler(KeyEvents.Decimal_KeyPress);
                txt.Width = 50;
                tabPanel.Controls.Add(txt);

                Label lb2 = new Label();
                lb2.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top ;
                lb2.TextAlign = ContentAlignment.MiddleLeft;
                lb2.Text = "元";
                lb2.Width = 20;
                tabPanel.Controls.Add(lb2);
            }
        }

        void InitializeDefault()
        {
            foreach (Control con in tabPanel.Controls)
            {
                if (con is TextBox)
                {
                    con.Text = "0";
                }
            }
        }
    }
}
